<template>
  <div class="avue-crud">
    <el-table :data="dataList" :height="tableHeight==null?'calc((100vh - 80px - 70px) / 2 - 200px)':tableHeight" style="min-height: 100px;">

      <el-table-column prop="edit_user_id" label="上传情况" width="160px" align="center">
        <template slot-scope="scope">
          <span :class="Zt(scope.row.fjdz)">{{ scope.row.szbgFjdz ? "已上传" : "未上传" }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="szLx" label="述职类型">
      </el-table-column>
      <el-table-column prop="szDeptName" label="述职部门">
      </el-table-column>
      <el-table-column prop="szUserName" label="述职人">
      </el-table-column>
      <el-table-column prop="szJzDate"  :formatter="formater" label="述职截止时间">
      </el-table-column>
      <el-table-column prop="shZt" label="审核状态">
      </el-table-column>
      <el-table-column prop="zhSm" show-overflow-tooltip label="审核说明">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
            <div  style="display: flex;">
          <el-upload v-if="scope.row.shZt=='审核中' || scope.row.shZt=='已退回'  ||  scope.row.shZt=='述职中'"
            class="upload-demo" accept=".pdf,.doc,.docx,.PDF" :headers="headers" :show-file-list="false"
            action="/admin/sys-file/upload" :on-success="handleUploadSuccess" :before-upload="beforeFileUpload "
            :on-change="handleChange" :on-preview="handlePictureCardPreview">
            <el-button size="small" @click="getSzbgId(scope.row)" type="primary">点击上传</el-button>
          </el-upload>
                 <el-button v-if="scope.row.szbgFjdz" type="primary" plain
            @click="handlePictureCardPreview(scope.row.szbgFjdz)">查看
          </el-button>
            </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="avue-crud__pagination">
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </div>
    <cepinghdgulszbgUploadForm v-if="cepinghdgulszbgUploadFormVisible" ref="cepinghdgulszbgUploadForm"
      @refreshSubList="getDataList"></cepinghdgulszbgUploadForm>

    <el-dialog title="述职报告" :visible.sync="bgdialogVisible" width="90vw" top="3vh" append-to-body>
      <iframe :src="bgUrl" style="width: 100%;height:75vh"></iframe>
      <span slot="footer" class="dialog-footer">
        <el-button @click="bgdialogVisible = false">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    updateBySzUserId,
    szrFetchList
  } from "@/api/ceping/cepingszbgb";

  import {
    getPreview
  } from "@/util/util";
  import store from '@/store'
  import cepinghdgulszbgUploadForm from './cepinghdgulszbg-upload-form'
  import {
    mapGetters
  } from 'vuex'

  export default {
    data() {
      return {
        dataForm: {
          key: ''
        },
        dataFormSzbg: {
          id: 0,
          szbgFjmc: '',
          szbgFjlx: '',
          szbgFjdz: '',
          szJzDate:''
        },
        bgdialogVisible: false,
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false,
        cepinghdgulszbgUploadFormVisible: false,
        headers: {
          Authorization: 'Bearer ' + store.getters.access_token
        },
        bgUrl: ""
      }
    },
    computed: {
      ...mapGetters(['permissions'])
    },
    created() {
      this.getDataList();
    },
    computed: {
      ...mapGetters(['permissions'])
    },
    props: {
      tableHeight: {
        type: String,
        default: null
      }
    },
    components: {
      cepinghdgulszbgUploadForm
    },
    methods: {
     formater(row, column, cellValue, index){
         if(cellValue){
        return this.$moment(cellValue).format("yyyy-MM-DD HH:mm");
      }else {
        return cellValue;
      }
      },
      // 获取数据列表 预览分页查询使用
      getDataList() {
        this.dataListLoading = true
        szrFetchList(Object.assign({
          current: this.pageIndex,
          size: this.pageSize
        })).then(response => {
          this.dataList = response.data.data.records
          this.totalPage = response.data.data.total
        })
        this.dataListLoading = false
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      //上传数值报告
      uploadFile(id, hdName) {
        this.cepinghdgulszbgUploadFormVisible = true;
        this.$nextTick(() => {
          this.$refs.cepinghdgulszbgUploadForm.init(id, hdName);
        });
      },

      //状态
      Zt(value) {

        if (value) {
          return 'yinCang';
        } else {
          return 'red'
        }

      },
      /**
       * 文件修改
       */
      handleChange(file, fileList) {
        this.fileList = [];
        this.fileList.push(file);
      },
      /**
       * 文件上传前
       */
      beforeFileUpload(file) {
        const isType = file.type === 'application/pdf' || file.type === 'application/msword' || file.type ===
          'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
        if (!isType) {
          this.$notify.closeAll();
          this.$notify.error('上传文件格式不对!')
          return false;
        }
        return isType;
      },
      /**
       * 文件上传成功
       */
      handleUploadSuccess(response, file, fileList) {
        if (response.code == 0) {
          this.dataFormSzbg.szbgFjdz = response.data.url;
          this.dataFormSzbg.szbgFjmc = file.name;
          this.dataFormSzbg.szbgFjlx = file.raw.type;
          updateBySzUserId(this.dataFormSzbg).then(data => {
            this.$notify.closeAll();
            this.$notify.success('上传成功');
            this.getDataList();
            this.$emit("reload");

          }).catch(() => {
            this.$notify.closeAll();
            this.$notify.error('上传失败');
          });

        } else {
          this.$notify.closeAll();
          this.$notify.error('上传出错：' + response.msg)
        }
      },
      getSzbgId(r) {
        this.dataFormSzbg.id = r.id;
        this.dataFormSzbg.szJzDate = r.szJzDate
      },
      /**
       * 文件预览预留
       */
      handlePictureCardPreview(url) {
        if (url) {
          this.bgUrl = getPreview(url) + "&officePreviewType=pdf";
          this.bgdialogVisible = true;
        } else {
          this.$notify.closeAll();
          this.$notify.error('还未上传附件');
        }

      },
    }
  }

</script>
